package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.userms.dto.GdbMerchantUploadAlbumDTO;
import com.yuanfeng.userms.dto.ManageImageGroupDeleteDTO;
import com.yuanfeng.userms.entity.UserUploadAlbumEntity;
import com.yuanfeng.userms.mapper.UserUploadAlbumMapper;
import com.yuanfeng.userms.service.UserUploadAlbumService;
import com.yuanfeng.userms.vo.ManageQueryGroupVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserUploadAlbumServiceImpl
 * @Description 用户相册
 * @Author cf
 * @Date 2023/6/28 16:47
 * @Version 1.0
 */
@Service
public class UserUploadAlbumServiceImpl extends ServiceImpl<UserUploadAlbumMapper, UserUploadAlbumEntity> implements UserUploadAlbumService {

    @Resource
    private UserUploadAlbumMapper userUploadAlbumMapper;

    @Override
    public ResponseResult queryImageGroup(UserTokenBeanDTO param) {
        List<ManageQueryGroupVO> queryGroups = new ArrayList<ManageQueryGroupVO>();
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_ALBUM,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            Map<String,Object> map = new HashMap<>();
            map.put("shopId",userPermissionsResult.getShopId());
            map.put("albumType",param.getAlbumType());
            queryGroups = userUploadAlbumMapper.selectByShopIdAndType(map);
            ManageQueryGroupVO queryGroup = new ManageQueryGroupVO();
            queryGroup.setAlbumId(0);
            queryGroup.setAlbumDesc("默认分组");
            queryGroups.add(0, queryGroup);

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success(queryGroups);
    }


    @Override
    @Transactional
    public ResponseResult createGroup(GdbMerchantUploadAlbumDTO param) {

        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_ALBUM,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            // 插入数据库
            param.setUserId(userPermissionsResult.getUserId());
            param.setShopId(userPermissionsResult.getShopId());

            if (param.getAlbumId() == null) {
                userUploadAlbumMapper.insertSelective(param);
            } else {
                userUploadAlbumMapper.updateByPrimaryKeySelective(param);
            }

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }


    @Override
    public ResponseResult deleteImageGroup(ManageImageGroupDeleteDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_ALBUM,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            userUploadAlbumMapper.deleteByPrimaryKey(param.getAlbumId());

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }
}
